package com.yic.module.device.controller.admin.inspectionPlanEntry;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.inspectionPlanEntry.vo.*;
import com.yic.module.device.dal.dataobject.inspectionPlanEntry.InspectionPlanEntryDO;
import com.yic.module.device.convert.inspectionPlanEntry.InspectionPlanEntryConvert;
import com.yic.module.device.service.inspectionPlanEntry.InspectionPlanEntryService;

@Tag(name = "管理后台 - 设备检查项")
@RestController
@RequestMapping("/device/inspection-plan-entry")
@Validated
public class InspectionPlanEntryController {

    @Resource
    private InspectionPlanEntryService inspectionPlanEntryService;

    @PostMapping("/create")
    @Operation(summary = "创建设备检查项")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-entry:create')")
    public CommonResult<Long> createInspectionPlanEntry(@Valid @RequestBody InspectionPlanEntryCreateReqVO createReqVO) {
        return success(inspectionPlanEntryService.createInspectionPlanEntry(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备检查项")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-entry:update')")
    public CommonResult<Boolean> updateInspectionPlanEntry(@Valid @RequestBody InspectionPlanEntryUpdateReqVO updateReqVO) {
        inspectionPlanEntryService.updateInspectionPlanEntry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备检查项")
    @Parameter(name = "id", description = "编号", required = true)
    @Parameter(name = "flag",description = "删除与设备关联信息",required = false)
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-entry:delete')")
    public CommonResult<Boolean> deleteInspectionPlanEntry(@RequestParam("id") Long id,@RequestParam("flag") Boolean flag) {
        inspectionPlanEntryService.deleteInspectionPlanEntry(id,flag);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备检查项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-entry:query')")
    public CommonResult<InspectionPlanEntryRespVO> getInspectionPlanEntry(@RequestParam("id") Long id) {
        InspectionPlanEntryDO inspectionPlanEntry = inspectionPlanEntryService.getInspectionPlanEntry(id);
        return success(InspectionPlanEntryConvert.INSTANCE.convert(inspectionPlanEntry));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备检查项列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-entry:query')")
    public CommonResult<List<InspectionPlanEntryRespVO>> getInspectionPlanEntryList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectionPlanEntryDO> list = inspectionPlanEntryService.getInspectionPlanEntryList(ids);
        return success(InspectionPlanEntryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备检查项分页")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-entry:query')")
    public CommonResult<PageResult<InspectionPlanEntryRespVO>> getInspectionPlanEntryPage(@Valid InspectionPlanEntryPageReqVO pageVO) {
        PageResult<InspectionPlanEntryDO> pageResult = inspectionPlanEntryService.getInspectionPlanEntryPage(pageVO);
        return success(InspectionPlanEntryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/not-select-page")
    @Operation(summary = "获得设备未选择检查项分页")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-entry:query')")
    public CommonResult<PageResult<InspectionPlanEntryRespVO>> getInspectionPlanEntryNotSelectPage(@Valid InspectionPlanEntryPageReqVO pageVO) {
        PageResult<InspectionPlanEntryDO> pageResult = inspectionPlanEntryService.getInspectionPlanEntryNotSelectPage(pageVO);
        return success(InspectionPlanEntryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备检查项 Excel")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-entry:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectionPlanEntryExcel(@Valid InspectionPlanEntryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectionPlanEntryDO> list = inspectionPlanEntryService.getInspectionPlanEntryList(exportReqVO);
        // 导出 Excel
        List<InspectionPlanEntryExcelVO> datas = InspectionPlanEntryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备检查项.xls", "数据", InspectionPlanEntryExcelVO.class, datas);
    }

}
